package com.qiantu.phone.widget;

import android.content.Context;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.lifecycle.LifecycleOwner;

import com.hjq.http.listener.HttpCallback;
import com.hjq.toast.ToastUtils;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.entity.DeviceState;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.ui.uiutil.LLDeviceUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;


/**
 * Created by luzx on 2021/12/16.
 * 首页调光大view
 */


public class HomeDimmingBigView extends HomeDeviceBaseView {
//    private TextView tvBigOffline;
    private TextView tvValueBig;
    private CustomSeekCurtain seekBar;
    private ImageView imgDeviceFlag;
    private DeviceState mDeviceState;
    private DeviceState.StatusDTO mStatus;

    public HomeDimmingBigView(Context context) {
        super(context, null);
    }

    public HomeDimmingBigView(Context context, int type) {
        super(context, type);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.home_big_dimming_item;
    }

    protected void initView() {
        isBigView(true);
        super.initView();
//        shapeLinearLayout.getShapeDrawableBuilder()
//        shapeLinearLayout.getShapeDrawableBuilder().setSolidColor(0xFF2F4D81);
        //bigView离线状态view重新赋值
//        tvBigOffline = findViewById(R.id.tv_offline_big);
        tvValueBig = findViewById(R.id.tv_value_big);
        seekBar = findViewById(R.id.seek_bar);
        imgDeviceFlag = findViewById(R.id.tv_out_device_flag);
        seekBar.setOnSeekBarChangeListener(new CustomSeekCurtain.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(int progress) {
                setProgress(progress);
            }

            @Override
            public void onStartTrackingTouch() {

            }

            @Override
            public void onStopTrackingTouch(int progress) {
                setProgress(progress);
                if(progress == 0){
//                    btnSwitch.setChecked(false);
                    Map<String, Object> option = new HashMap<>();
                    option.put(LLDeviceUtil.POWER, 0);
                    setLabelColor(false);
                    switchOperation(option);
                }else{
//                    DeviceState deviceState = mDevice.getStateData();
//                    Integer light = deviceState.getStatus().getLight();
//                    if (Float.valueOf(light) != progress) {
//                        lightOperation(progress);
//                    }
                    setLabelColor(true);
                    lightOperation(progress);
                }
            }
        });
    }

    private void setProgress(int progress){
        if(progress == 0){
//            btnSwitch.setChecked(false);
            tvValueBig.setText(R.string.close);
//            tvValueBig.setTextColor(getResources().getColor(AppApplication.getInstance().getThemeColor(R.attr.secondaryColor4)));
        }else{
//            btnSwitch.setChecked(true);
//            tvValueBig.setTextColor(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
            tvValueBig.setText(progress + "%");
        }
    }

    private void setStatusBySwitch(boolean isOpen){
        seekBar.setOpen(isOpen);
        if (isOpen) {
//            //开状态，亮度0就设置到默认100
//            if(seekBar.getProgress() == 0){
//                seekBar.setProgress(100);
//            }
            Integer light = mStatus.getLight();
            Float progress = Float.valueOf(light);
            seekBar.setProgress(progress);
            tvValueBig.setText(seekBar.getProgressPercentage() + "%");
        } else {
            tvValueBig.setText(R.string.close);
        }
    }

    //重载开关控制
    protected void switchOperation(){
        int power;
        if(mDevice.getIsOpen()){
            power = 0;
        }else{
            power = 1;
        }
        Map<String, Object> option = new HashMap<>();
        option.put(LLDeviceUtil.POWER, power);
        switchOperation(option);
    }

    @Override
    protected void onSwitchOperationSucceed(boolean isOpen) {
        setStatusBySwitch(isOpen);
        mDevice.setIsOpen(isOpen);
//        mDevice.setLight((int)seekBar.getProgress() + "");
        LLDeviceDBManager.getInstance(getContext()).put(mDevice);
    }

    protected void updateView() {
        super.updateView();
        mDeviceState = mDevice.getStateData();
        mStatus = mDeviceState.getStatus();
//        if (mDevice.isIsOnline()) {
//            tvBigOffline.setVisibility(GONE);
//            tvValueBig.setVisibility(VISIBLE);
            setStatusBySwitch(mDevice.getIsOpen());
//        } else {
//            tvBigOffline.setVisibility(VISIBLE);
//            tvValueBig.setTextColor(GONE);
//        }
//        seekBar.isOnline(mDevice.isIsOnline());

        imgDeviceFlag.setVisibility(mDevice.getIsFavourite() ? VISIBLE : INVISIBLE);
    }

    private void lightOperation(int light) {
        Map<String, Object> option = new HashMap<>();
        option.put(LLDeviceUtil.LIGHT, light);
//        boolean isOpen = light > 0;
//        option.put("power", isOpen ? 1 : 0);
        Map<String, Object> status = new HashMap<>();
        status.put(LLDeviceUtil.STATUS, option);
        HttpCallback<HttpData<Void>> callback = new HttpCallback<HttpData<Void>>(null) {
            @Override
            public void onStart(Call call) {
                if(getContext() != null && getContext() instanceof AppActivity) {
                    ((AppActivity) getContext()).showDialog("执行中...");
                }
            }

            @Override
            public void onSucceed(HttpData<Void> result) {
//                ToastUtils.show("执行成功");
                mDevice.setLight(light);
                boolean isOpen = light > 0;
                if(isOpen != mDevice.getIsOpen()){
                    mDevice.setIsOpen(isOpen);
                    setStatusBySwitch(mDevice.getIsOpen());
                }
                LLDeviceDBManager.getInstance(getContext()).put(mDevice);
            }

            @Override
            public void onFail(Exception e) {
                //失败取反
                //取设备原来亮度
                float progress = Float.valueOf(mStatus.getLight());
                seekBar.setProgress(progress);
                tvValueBig.setText(seekBar.getProgressPercentage() + "%");
                ToastUtils.show(R.string.exec_failure);
            }

            @Override
            public void onEnd(Call call) {
                if(getContext() != null && getContext() instanceof AppActivity) {
                    ((AppActivity) getContext()).hideDialog();
                }
            }
        };
        if(mDevice.getDeviceGroup() == 1){
            LLHttpManager.operationDeviceGroup((LifecycleOwner) getContext(), mDevice.getDeviceSerialNo(), status, callback);
        }else{
            LLHttpManager.operationDevice((LifecycleOwner) getContext(), mDevice.getDeviceSerialNo(), status, callback);
        }
    }
}
